<?php
	//fil d'ariane
	$ariane = array(
		array(
			'href' => "index.php",
			'name' => "Home"
		),
		array(
			'href' => "adm_home.php",
			'name' => "Admin"
		),
		array(
			'href' => "adm_players.php",
			'name' => "Players moderation"
		)
	);
	
	require("include/includes.php");
	require("include/getIP.php");
	
	// CHECK IF THE USER CAN SEE THE PAGE
	if(!checkRights(VALIDATOR)) //TODO
	{
		die('Permission denied');
	}
	
	// Valeurs initiales
	$valueTrack 		= postOrCustom('track', '');
	$isAPlayer = false;
	$pid=0;
	$players = array();
	$player = null;
	$errors = Array();
	//$records = Record::getAllForState(PENDING);
	$records = Record::getAllForState(REFUSED);
	
	/// --- VALIDATION DU FORMULAIRE ---
	if(isset($_GET['pid']) && is_numeric($_GET['pid']))
	{
		$pid=$_GET['pid'];
		$isAPlayer = true;
		$player = new Player($pid);
		//TODO Player exists?
		
		if(isset($_POST['submit']))
		{
			if(!verifyCsrfToken())
			{
				$errors['csrf'] = 'Security token missing!';
			}
			
			if(empty($errors)) // CSRF ok
			{
				switch($_POST['submit'])
				{
					case 'Unban':
						$player->unbann();
						break;
					
					case 'Ban':
						if(isset($_POST['bandays']) && is_numeric($_POST['bandays']))
						{
							$nbDays = $_POST['bandays'];
							if($nbDays > 0)
							{
								$player->bann($nbDays);
							}
						}
						break;
				}
			}
		}
	}
	else
	{
		$players = Player::getAll();
	}
	
	$template = $twig->loadTemplate('admin/players.html');
	$template->display(array(
		'pageTitle' 		=> 'Players moderation',
		'isAPlayer'			=> $isAPlayer,
		'players'			=> $players,
		'player'			=> $player,
		'errors'			=> $errors
	));
?>